package com.yby.es.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.yby.es.entity.User;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * es 简单增删改查api使用
 */
@RestController
@RequestMapping("/es")
public class EsController {

    @Autowired
    private Client client;

    private final static String INDEX= "test-index";
    private final static String TYPE= "default";
    //private final static String INDEX= "";

    @GetMapping("/getEs")
    public List<Map<String,Object>> getEs(@RequestParam("name") String name){
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if (StrUtil.isNotBlank(name)) {
            //queryBuilder.must(QueryBuilders.termQuery("name", name));
        }
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX).setTypes(TYPE).setQuery(queryBuilder)
                .setFetchSource(true) // true 返回数据  false 只返回id
                .setFrom(0).setSize(10);
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(10, TimeUnit.SECONDS);
        List<Map<String,Object>> result = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().hits()) {
            result.add(hit.getSource());
        }
        return result;
    }

    @RequestMapping("/saveEs")
    public String saveEs(){
        User user = new User();
        user.setAge(1);
        user.setName("张三");
        user.setSex("女");
        user.setId(111L);
        User user2 = new User();
        user2.setAge(88);
        user2.setName("里斯");
        user2.setSex("男");
        user2.setId(222L);
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex()
                .setSource(JSON.toJSONString(user2), XContentType.JSON)
                .setIndex(INDEX)
                .setType(TYPE)
                .setId(user2.getId().toString())
                .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        indexRequestBuilder.execute();
        return "插入成功";
    }

    @RequestMapping("/updateEs")
    public String updateEs(){
        User user = new User();
        user.setAge(1);
        user.setName("张三1号");
        user.setSex("女1号");
        user.setId(111L);

        client.prepareUpdate(INDEX, TYPE, user.getId() + "")
                .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
                .setDoc(BeanUtil.beanToMap(user))
                .execute();
        return "更新成功";
    }

    @GetMapping("/deleteEs")
    public String updateEs(@RequestParam String id){
        User user = new User();
        user.setAge(1);
        user.setName("张三1号");
        user.setSex("女1号");
        user.setId(111L);

        client.prepareDelete(INDEX, TYPE, id)
                .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
                .execute();
        return "删除成功";
    }


}
